import 'package:flutter/material.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_api.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_category.dart';
import 'package:schedule_self_client/utils/device_size_util.dart';

class TaskCategorySelectDialog extends StatefulWidget {
  const TaskCategorySelectDialog({super.key});

  @override
  State<TaskCategorySelectDialog> createState() =>
      _TaskCategorySelectDialogState();
}

class _TaskCategorySelectDialogState extends State<TaskCategorySelectDialog> {
  TaskCategory? _taskCategory;
  final List<TaskCategory> _taskCategoryList = [];

  @override
  void initState() {
    super.initState();
    _loadTaskCategoryList();
  }

  @override
  Widget build(BuildContext context) {
    double dialogWidth = DeviceSizeUtil.getScreenWidth(context) * 0.8;

    return AlertDialog(
      title: const Text('请选择一个任务分类'),
      content: SizedBox(
        width: dialogWidth,
        child: ListView.builder(
            itemCount: _taskCategoryList.length,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  setState(() {
                    _taskCategory = _taskCategoryList[index];
                  });
                },
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: Container(
                    padding: const EdgeInsets.all(8.0),
                    decoration: BoxDecoration(
                      color: Color(int.parse(_taskCategoryList[index].color)),
                      borderRadius: BorderRadius.circular(8.0),
                      border: Border.all(
                          width: _taskCategory != null &&
                                  _taskCategory!.id ==
                                      _taskCategoryList[index].id
                              ? 4
                              : 1),
                    ),
                    child: Text(
                      _taskCategoryList[index].name,
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                  ),
                ),
              );
            }),
      ),
      actions: [
        TextButton(
          style: TextButton.styleFrom(
            textStyle: Theme.of(context).textTheme.labelLarge,
          ),
          child: const Text('取消'),
          onPressed: () {
            Navigator.of(context).pop(null);
          },
        ),
        TextButton(
          style: TextButton.styleFrom(
            textStyle: Theme.of(context).textTheme.labelLarge,
          ),
          child: const Text('确定'),
          onPressed: () {
            Navigator.of(context).pop(_taskCategory);
          },
        ),
      ],
    );
  }

  Future<void> _loadTaskCategoryList() async {
    final taskCategoryListResp = await TaskApi.getTaskCategoryList();
    setState(() {
      _taskCategoryList.addAll(taskCategoryListResp.data);
    });
  }
}
